package com.acce.base.utils;

import java.io.BufferedReader;
//import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Map;

//import org.apache.http.HttpEntity;
//import org.apache.http.NameValuePair;
//import org.apache.http.client.entity.UrlEncodedFormEntity;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.message.BasicNameValuePair;
//import org.apache.http.util.EntityUtils;
//import org.apache.log4j.Logger;

//import org.apache.axiom.om.OMAbstractFactory;
//import org.apache.axiom.om.OMElement;
//import org.apache.axiom.om.OMFactory;
//import org.apache.axiom.om.OMNamespace;
//import org.apache.axis2.AxisFault;
//import org.apache.axis2.addressing.EndpointReference;
//import org.apache.axis2.client.Options;
//import org.apache.axis2.client.ServiceClient;

public class RemoteUtils {
	
//	private static Logger logger = Logger.getLogger(RemoteUtils.class);

//	public static String remote(String serviceAddr, String namesapce, String methodName, String requestMsg) throws Exception{
//		String restr = null;
//		EndpointReference targetEPR = new EndpointReference(serviceAddr);
//		Options options = new Options();
//		options.setAction("urn:" + methodName) ;
//		options.setTo(targetEPR);
//		ServiceClient sender = null;
//		try {
//			sender = new ServiceClient();
//			sender.setOptions(options);
//			OMFactory fac = OMAbstractFactory.getOMFactory();
//			OMNamespace omNs = fac.createOMNamespace(namesapce, "");
//			OMElement method = fac.createOMElement(methodName, omNs);
//			OMElement name = fac.createOMElement("requestMsg", omNs);
//			name.setText(requestMsg);
//			method.addChild(name);
//			method.build();
//			OMElement response = sender.sendReceive(method);
////			System.out.println(response);
////			OMElement elementxx = response.getFirstElement();
////			System.out.println("xxxx:" + elementxx.getText());
////			OMElement element = response.getFirstChildWithName(new QName("accecard", "return"));
////			System.out.println(element.getText());
//			OMElement element = response.getFirstElement();
//			restr = element.getText();
//		} catch (AxisFault e) {
//			throw new Exception(e.getMessage());
//		}finally{
//			if(null != sender){
//				try {
//					sender.cleanupTransport();
//				} catch (AxisFault e) {
//					throw new Exception(e.getMessage());
//				}
//			}
//		}
//		return restr;
//	}
	
	
	public static boolean httpRemoteTest(String serviceAddr, int timeout) throws Exception{
		HttpURLConnection connection = null;
		boolean flag = false;
		try {
			URL url = new URL(serviceAddr);
			connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(timeout);
			connection.connect();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(null != connection){
				connection.disconnect();
			}
		}
		return flag;
	}
	
	public static String httpRemote(String serviceAddr, String param) throws Exception{
		HttpURLConnection connection = null;
		BufferedReader br = null;
		StringBuffer buff = new StringBuffer();
//		boolean flag = false;
		try {
			URL url = new URL(serviceAddr);
			connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(5000);
//			connection.connect();
			
			connection.setDoInput(true);  
			connection.setDoOutput(true);
			connection.setRequestProperty("Accept-Language", "zh-CN");
			connection.setRequestProperty("Charset", "utf-8");
			connection.setRequestMethod("POST");
            
			connection.setRequestProperty("Content-Length",  
                    String.valueOf(param.getBytes().length));  
			connection.setUseCaches(false);  
            //����Ϊgbk���Խ������������ʱ��ȡ������������������  
			connection.getOutputStream().write(param.getBytes("utf-8"));  
			connection.getOutputStream().flush();  
			connection.getOutputStream().close();  
            BufferedReader in = new BufferedReader(new InputStreamReader(  
            		connection.getInputStream(), "UTF-8"));  
			
			// ��ȡ������
//            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {// ѭ����ȡ��
            	buff.append(line);
            }
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(null != connection){
				connection.disconnect();
			}
			if(null != br){
				br.close();// �ر���
			}
		}
		return buff.toString();
	}
	
	public static void httpRemote(String serviceAddr, String param, boolean sync) throws Exception{
		HttpURLConnection connection = null;
//		BufferedReader br = null;
		try {
			URL url = new URL(serviceAddr);
			connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(5000);
//			connection.connect();
			
			connection.setDoInput(true);  
			connection.setDoOutput(true);
			connection.setRequestProperty("Accept-Language", "zh-CN");
			connection.setRequestProperty("Charset", "utf-8");
			connection.setRequestMethod("POST");
            
			connection.setRequestProperty("Content-Length",  
                    String.valueOf(param.getBytes().length));  
			connection.setUseCaches(false);  
            //����Ϊgbk���Խ������������ʱ��ȡ������������������  
			connection.getOutputStream().write(param.getBytes("utf-8"));  
			connection.getOutputStream().flush();  
//			connection.getOutputStream().close();  
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(null != connection){
				connection.disconnect();
			}
			
		}
	}
	
	public static void main(String[] args) throws Exception{
//		JSONObject json = new JSONObject();
//		UserList ul = new UserList();
//		ul.setAtUserId("KL1604266122521");
//		ul.setCompanyId("CP1604266458656");
//		ul.setCompanyName("麻黄");
//		ul.setPhoneNum("15327198111");
//		ul.setToken("5176da337612ed372e1f0a57c41a1763a8cb122d");
//		UserPassword up = new UserPassword();
//		up.setPasswordId("UP1604266122521");
//		up.setAtUserId("KL1604266122521");
//		up.setPhoneNum("15327198111");
//		up.setUserPwd("fc303cd21672b55e48068d6025d5c5ae");
//		up.setToken("5176da337612ed372e1f0a57c41a1763a8cb122d");
//		up.setOpenId("12j3y12oiuy3ou12t87y2t3287e3y27g32ui4u3i2");
//		CompanyList cl = new CompanyList();
		
		
		
//		cl.setCompanyId("CP1604266458656");
//		cl.setCompanyName("麻黄");
//		cl.setCompanyType("1");
//		cl.setAdminId("KL1604266122521");
//		cl.setAdminName("黄麻");
//		cl.setAdminPhone("15327198111");
//		cl.setAdminPwd("fc303cd21672b55e48068d6025d5c5ae");

//		json.put("atUserId", "KL1604266122521");
//		json.put("token", "5176da337612ed372e1f0a57c41a1763a8cb122d");
//		json.put("us", ul); //CP1604266458656  CP1629435478540 CP1635179826016
//		json.put("pw", up);
//		json.put("cp", cl); //1 2 0
//		String serviceAddr = "http://192.168.48.130:8080/nsy-product/api/product/centerService?";
//		String param = "serviceCode=3001P08&business=";
//		param += json.toJSONString();
//		param += "&device=SM-G9008V&editUser=yangjian&editTime=20160721100157&sign=b80ab5e71c96a1dd54063f06709109839dff66cc&ipaddr=192.168.0.95&registerType=p&mac=C0%3ABD%3AD1%3A92%3A20%3AC1&timestamp=20160721100157&nonce=3333333";
//		boolean flag = httpRemoteTest(serviceAddr, 3000);
//		if(flag){
//			System.out.println("连接成功");
////			System.out.println(httpRemote(serviceAddr, param));
//			httpRemote(serviceAddr, param);
//		}
//		
//		for(int i = 0; i < 1000; i++){
//			System.out.println("333333333333333333333");
//		}
//		
		
//		JSONObject json = new JSONObject();
//		json.put("companyId", "CP1629435478540"); //CP1604266458656  CP1629435478540 CP1635179826016
//		json.put("companyName", "麻黄");
//		json.put("companyType", "2"); //1 2 0
//		String serviceAddr = "http://192.168.49.13:8080/nsy-product/api/product/centerService?";
//		String param = "serviceCode=3001P01&business=";
//		param += json.toJSONString();
//		param += "&device=SM-G9008V&editUser=yangjian&editTime=20160721100157&sign=381cb5884e31d69770085a39980291cc06c6c1d7&ipaddr=192.168.0.95&registerType=p&mac=C0%3ABD%3AD1%3A92%3A20%3AC1&timestamp=20160721100157&nonce=3333333";
//		boolean flag = httpRemoteTest(serviceAddr, 3000);
//		
//		if(flag){
//			System.out.println("连接成功");
//			System.out.println(httpRemote(serviceAddr, param));
//		}
		
	}
}
